package cn.walls1717.mall.modules.pms.controller;


import cn.walls1717.mall.common.api.CommonPage;
import cn.walls1717.mall.common.api.CommonResult;
import cn.walls1717.mall.common.util.ComResultUtil;
import cn.walls1717.mall.modules.pms.dto.PageListDTO;
import cn.walls1717.mall.modules.pms.model.PmsBrand;
import cn.walls1717.mall.modules.pms.service.PmsBrandService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author walls1717
 * @since 2022-04-09
 */
@RestController
@RequestMapping("/brands")
public class PmsBrandController {

    private final PmsBrandService brandService;

    @Autowired
    public PmsBrandController(PmsBrandService brandService) {
        this.brandService = brandService;
    }

    private PmsBrand brand;

    @ApiOperation("品牌列表")
    @GetMapping()
    public CommonResult<CommonPage<PmsBrand>> pageList(@Valid PageListDTO pageListDTO) {
        Page<PmsBrand> list = brandService.pageList(pageListDTO);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("品牌添加")
    @PostMapping()
    public CommonResult<?> createBrand(@RequestBody PmsBrand brand) {
        boolean result = brandService.save(brand);
        return ComResultUtil.booleanResult(result);
    }

    @ApiOperation("品牌删除")
    @DeleteMapping("{id}")
    public CommonResult<?> deleteBrand(@PathVariable("id") Long id) {
        boolean result = brandService.removeById(id);
        return ComResultUtil.booleanResult(result);
    }

    @ApiOperation("品牌编辑页面数据填充")
    @GetMapping("{id}")
    public CommonResult<?> getOneBrand(@PathVariable("id") Long id) {
        PmsBrand brand = brandService.getById(id);
        return CommonResult.success(brand);
    }

    @ApiOperation("品牌编辑")
    @PutMapping("{id}")
    public CommonResult<?> updateBrand(@PathVariable("id") Long id, @RequestBody PmsBrand brand) {
        boolean result = brandService.updateById(brand);
        return ComResultUtil.booleanResult(result);
    }

    @ApiOperation("品牌品牌制造商状态编辑")
    @PutMapping("factoryStatus")
    public CommonResult<?> updateFactoryStatus(@RequestParam("ids") List<Long> ids, @RequestParam("factoryStatus") Integer factoryStatus) {
        brand = new PmsBrand();
        brand.setFactoryStatus(factoryStatus);
        Boolean result = brandService.updateStatus(ids, brand);
        return ComResultUtil.booleanResult(result);
    }

    @ApiOperation("品牌是否显示状态编辑")
    @PutMapping("showStatus")
    public CommonResult<?> updateShowStatus(@RequestParam("ids") List<Long> ids, @RequestParam("showStatus") Integer showStatus) {
        brand = new PmsBrand();
        brand.setShowStatus(showStatus);
        Boolean result = brandService.updateStatus(ids, brand);
        return ComResultUtil.booleanResult(result);
    }
}

